package game.Actors;
import game.Config.Config;

import java.util.ArrayList;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Vector2f;
/**
 * This class represents a rocket bullet in the game.
 * A rocket bullet in the game will explode when it collides with an enemy, and hurt
 * more enemies that are nearby.
 * @author Sebastian Isheden and Victor Beer.
 */
public class RocketBullet extends Bullet{
	private float explosionRadius;
	private SpriteSheet explosionAnimation;
	/**
	 * Creates a rocketbullet based on the given parameters.
	 * @param image The image of the bullet.
	 * @param position The position of the bullet.
	 * @param speed The speed of the bullet.
	 * @param direction The direction of the bullet. 
	 * @param damage The damage of the bullet.
	 * @param explosionRadius The radius of the explosion upon impact.
	 * @param explosionAnimation The animation that will occur upon impact.
	 */
	public RocketBullet(Image image, Vector2f position, float speed,Vector2f direction, float damage,float explosionRadius,SpriteSheet explosionAnimation) {
		super(image, position, speed, direction, damage);
		this.setImageRotation();
		this.explosionRadius = explosionRadius;
		this.explosionAnimation = explosionAnimation;
	}
	@Override
	protected void explode(Enemy enemy){      
		int enemiesDamaged = 1;
		this.isDead = true;
		new AnimationActor(this.position.copy(),this.explosionAnimation);
		ArrayList<Enemy> enemies = Enemy.getEnemyList();
		for(int i = enemies.size() - 1; i >= 0; i--) {
			if(enemiesDamaged >= Config.ROCKET_MAXIMUM_TARGETS){
				break;
			}
			Vector2f enemyPosition = enemies.get(i).position.copy();
			float distance = this.position.copy().add(new Vector2f(-enemyPosition.x,-enemyPosition.y)).length();
			if(distance <= this.explosionRadius && !enemy.equals(enemies.get(i))){
				float damageModifier = 1-distance/this.explosionRadius;
				enemies.get(i).hit(this.damage*damageModifier);
				enemiesDamaged++;
			}
		}
	}
	@Override
	protected void blow(){
		this.isDead = true;
		new AnimationActor(this.position.copy(),this.explosionAnimation);	
	}
}
